<template>
    <div class="role">
        <!--        添加/修改角色-->
        <el-dialog
                class="dialog-center"
                :title="addForm.id?'修改角色':'新增角色'"
                :visible.sync="addForm.visible"
                :close-on-click-modal="false"
                width="30%">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="150px">
                <el-form-item label="角色名称" prop="name" style="text-align:left">
                    <el-input v-model="addForm.name" size="mini" style="width:70%;"
                              placeholder="角色名称"></el-input>
                </el-form-item>
                <el-form-item label="是否可查" prop="queried" style="text-align:left">
                    <el-select v-model="addForm.queried" placeholder="注册账号时，是否需要查询出来" style="width: 70%">
                        <el-option label="可查" :value="true"></el-option>
                        <el-option label="不可查" :value="false"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="是否需要创建机构" prop="needOrg" style="text-align:left">
                    <el-select v-model="addForm.needOrg" placeholder="该角色下，是否需要创建机构" style="width: 70%">
                        <el-option label="需要" :value="true"></el-option>
                        <el-option label="不需要" :value="false"></el-option>
                    </el-select>
                </el-form-item>


            </el-form>
            <div align="center">
                <el-button type="primary" @click="submitForm('addForm')" size="small">{{addForm.id?'提交修改':'立即创建'}}
                </el-button>
                <el-button @click="resetForm('addForm')" size="small">重置</el-button>
            </div>

        </el-dialog>
        <el-row type="flex" justify="start">
            <el-col :span="24" style="text-align:left">
                <el-form :inline="true" :model="searchParam" class="demo-form-inline">
                    <el-form-item label="角色名称">
                        <el-input v-model="searchParam.name" placeholder="角色名称" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchRoleLists()">查询
                        </el-button>
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="showAddRoleDialog()">添加角色
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table
                        ref="multipleTable"
                        :data="tableData"
                        tooltip-effect="dark"
                        highlight-current-row
                        style="width: 100%">
                    <el-table-column
                            prop="name"
                            label="名称"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            label="是否可查"
                            align="center"
                            show-overflow-tooltip>
                        <template slot-scope="scope">{{scope.row.queried?'可查':'不可查'}}</template>
                    </el-table-column>
                    <el-table-column
                            label="需要创建机构"
                            align="center"
                            show-overflow-tooltip>
                        <template slot-scope="scope">{{scope.row.needOrg?'需要':'不需要'}}</template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="150" fixed="right" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="warning" @click="deleteRole(scope.row)">删除
                            </el-button>
                            <el-button size="mini" type="primary" @click="modifyRole(scope.row)">修改
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import CommonUtils from "@/utils/CommonUtils";
    import {addRole, updateRole, allRoleList,deleteRole} from "@/api/roleApi";

    export default {
        name: "role",
        data() {
            return {
                searchParam: {
                    name: null
                },
                tableData: [],
                addForm: {
                    visible: false,
                    id: null,
                    name: null,
                    queried: null,
                    needOrg: null
                },
                rules: {
                    name: [{required: true, message: '请输入角色名称', trigger: 'blur'}],
                    queried: [{required: true, message: '请选择', trigger: 'blur'}],
                    needOrg: [{required: true, message: '请选择', trigger: 'blur'}],
                }
            }
        },
        created() {
            this.searchRoleLists();
        },
        methods: {
            //查询角色列表
            searchRoleLists() {
                allRoleList(this.searchParam).then(res => {
                    this.tableData = res;
                })
            },

            //显示添加角色dialog
            showAddRoleDialog() {
                this.addForm = CommonUtils.clearObjectValue(this.addForm);
                this.addForm.visible = true;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //新增
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.addForm.id ? this.updateOneRole() : this.addOneRole();

                    } else {
                        this.$message.warning("请将信息填写完整");
                    }
                });
            },
            //新增一条角色
            addOneRole() {
                addRole(this.addForm).then(res => {
                    this.$message.success("创建成功");
                    this.addForm.visible = false;
                    this.searchRoleLists();
                })
            },
            //修改一条角色
            updateOneRole() {
                updateRole(this.addForm).then(res => {
                    this.$message.success("修改成功");
                    this.addForm.visible = false;
                    this.searchRoleLists();
                })
            },
            //进入修改
            modifyRole(row) {
                this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                this.addForm.visible = true;
            },
            //删除
            deleteRole(row) {
                this.$confirm('角色删除后将无法恢复，是否确定删除？', '重要提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let param = {id: row.id}
                    deleteRole(param).then(res => {
                        this.$message.success("删除成功");
                        this.searchRoleLists();
                    }, err => {
                    })
                }).catch(() => {
                })
            }
        }
    }
</script>

<style scoped>
    .role {

    }

</style>